﻿package org.openChart.meta.manager 
{
	import flash.events.EventDispatcher;
	import flash.text.StyleSheet;
	import org.openChart.meta.Axis;
	import org.openChart.meta.Style;
	import org.openChart.meta.TextLabel;
	import org.openChart.meta.Stick;
	import org.openChart.meta.StickLine;
	/**
	 * The base of classes which are designed to deal with XML files.
	 * 
	 * @author Chongyuan
	 */
	public class XmlBase  extends EventDispatcher
	{
		
		public function XmlBase()
		{
			
		}
		
		/**
		 * Build labels from xml.
		 * 
		 * @param	xml
		 * @param	parentStyle
		 * @return
		 */
		public function buildLabels( xml:XML, parentStyle:Style):Array
		{
			var labels:Array = new Array();
			var style:Style = buildStyle(xml, parentStyle);
			for each( var labelXml:XML in xml.label)
			{
				
				var label:TextLabel = buildLabel(labelXml, style);
				labels.push(label);
			}
			return labels;
		}
		
		/**
		 * Build a label from xml.
		 * 
		 * @param	xml
		 * @param	parentStyle
		 * @return
		 */
		public function buildLabel( xml:XML, parentStyle:Style):TextLabel
		{
			var style:Style = this.buildStyle(xml, parentStyle);
			if (xml && xml != "")
			{
				var label:TextLabel = new TextLabel(style, new String(xml));
				return label;
			}
			return null;
		}
		
		public function buildAxis( xml:XML, parentStyle:Style):Axis
		{
			var object:Object = CSSLoader.instance().getStyle(CSSLoader.axis_2d);
			var style:Style = new Style(object, parentStyle);
			style = this.buildStyle(xml, style);
			var axis:Axis = new Axis(style);
			
			if (new String(xml.@min) != "")
				axis.minValue = xml.@min;
				
			if (new String(xml.@max) != "")
				axis.maxValue = xml.@max;
				
			if (new String(xml.@step) != "")
				axis.step = xml.@step;
				
				
			object = CSSLoader.instance().getStyle(CSSLoader.axis_2d_labels);
			var labelStyle:Style = new Style(object, style);
			axis.initLabels(labelStyle);
			
			object = CSSLoader.instance().getStyle(CSSLoader.axis_2d_title);
			style = new Style(object, style);
			var title:TextLabel = null;
			if (new String(xml.title) != "")
				title = buildLabel(xml.title[0], style);
			axis.title = title;
			
			var sticks:Array = this.buildSticks(xml.sticks[0], axis.getStyle());
			axis.sticks = sticks;
			return axis;
		}
		
		public function buildSticks(xml:XML, parentStyle:Style):Array
		{
			var sticks:Array = new Array();
			var stickObject:Object = CSSLoader.instance().getStyle(CSSLoader.axis_2d_stick);
			
			var stickLineObject:Object = CSSLoader.instance().getStyle(CSSLoader.axis_2d_stick_line);
			var stickStyle:Style = new Style(stickObject, parentStyle);
			
			if (new String(xml) != "")
			{
				for each (var stickXml:XML in xml.stick) 
				{
					var stickStyleFinal:Style = this.buildStyle(stickXml, stickStyle);
					var stick:Stick = new Stick(stickStyleFinal, 1, false);
					if (new String(stickXml.@repeat) != "")
						stick.repeat = stickXml.@repeat;
					if (new String(stickXml.@showStickLine) != "")
						stick.showStickLine = stickXml.@showStickLine;
					var stickLineXml:XML = stickXml.stickLine[0];
					var stickLineStyle:Style = new Style(stickLineObject, stickStyleFinal);
					if (new String(stickLineXml) != "")
					{
						stickLineStyle = this.buildStyle(stickLineXml, stickLineStyle);	
					}
					var stickLine:StickLine = new StickLine(stickLineStyle);
					stick.stickLine = stickLine;
					sticks.push(stick);
				}
			} 
			else
			{
				var stick1:Stick = new Stick(stickStyle, 1, false);
				var stickLineStyle1:Style = new Style(stickLineObject, stickStyle);
				var stickLine1:StickLine  = new StickLine(stickLineStyle1);
				stick1.stickLine = stickLine1;
				sticks.push(stick1);
			}
			return sticks;
		}
		
		/**
		 * Build the style from xml.
		 * 
		 * @param	xml
		 * @param	parentStyle
		 * @return
		 */
		public function buildStyle( xml:XML, parentStyle:Style ):Style
		{
			
			var style:Style = new Style(null, parentStyle);
			if (xml)
			{
				
				if (new String(xml.@alpha) != "")
					style.alpha = xml.@alpha;
				if (new String(xml.@color)!= "")
					style.color = xml.@color;
				if (new String(xml.@fontFamily) != "" )
					style.fontFamily = xml.@fontFamily;
				if (new String(xml.@fontSize) !="")
					style.fontSize = xml.@fontSize;
				if (new String(xml.@selectedAlpha) != "")
					style.selectedAlpha = xml.@selectedAlpha;
				if (new String(xml.@selectedColor) != "")
					style.selectedColor = xml.@selectedColor;
				if (new String(xml.@selectedSize) != "")
					style.selectedSize = xml.@selectedSize;
				if (new String(xml.@size) != "")
					style.size = xml.@size;
				if (new String(xml.@height) != "")
					style.height = xml.@height;
				if (new String(xml.@width) != "") 
					style.width = xml.@width;
				if (new String(xml.@bold)!= "")
					style.width = xml.@bold;
				if (new String(xml.@showValue) != "")
					style.showValue = xml.@showValue;
				if (new String(xml.@showLine) != "")
					style.showLine = xml.@showLine;
				if (new String(xml.@offset) != "")
					style.offset = xml.@offset;
				if (new String(xml.@viewAngle) != "")
					style.viewAngle = xml.@viewAngle;
				if (new String(xml.@thickness) != "")
					style.thickness = xml.@thickness;
			}
			return style; 
		}
		
	}
	
}